/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
/*
 * Created on 24 oct. 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package fr.jade.console.cmd;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.objectweb.fractal.util.Fractal;

import fr.jade.console.core.AbstractCommand;
import fr.jade.console.core.Command;
import fr.jade.console.core.Environment;

/**
 * @author Sylvain Sicard
 */
public class Lc extends AbstractCommand implements Command{

  public String getDescription(){
    return "prints the life cycle state of the current component";
  }
  
  public String getHelp() {
    return "lc Prints curent life cycle state\n"+
           "lc -start Starts curent component\n"+
           "lc -stop Stops curent component";
  }
  
  public String getName() {
    return "lc";
  }
  
	public Object execute(String[] args, Environment env) throws Throwable {
		
		Component cmp = getCurrent(env);
		try{
  		LifeCycleController lcc = Fractal.getLifeCycleController(cmp);
  		if(args.length == 1){
  		  System.out.println(lcc.getFcState());
  		}else{
  		  if(args[1].equals("-start")){
  		    lcc.startFc();
  		  }else if(args[1].equals("-stop")){
  		    lcc.stopFc();
  		  }else{
  		    System.err.println(args[1]+": option unknown");
  		    System.err.println(getHelp());
  		  }
  		}
		}catch(NoSuchInterfaceException ignored){
		  System.err.println("No LCC on curent component");
		}
		return null;
	}
}
